package lc;

import org.junit.Test;

public class Ex9 {
    class Solution {
        public boolean isPalindrome(int x) {
            if (Math.abs(x) < 10) { 
                return true;
            }
            if (x < 0) {
                x = -x;
            }
            int cur = x, n = 0;
            while (cur != 0) {
                cur /= 10;
                n++;
            }
            cur = x;
            for (int i = 0; i < n / 2; i++) {
                int t1 = getIdx(cur, i);
                int t2 = getIdx(cur, n - i - 1);
                if (t1 != t2) {
                    return false;
                }
            }
            return true;
        }

        private int getIdx(int n, int idx) {
            int i = 0;
            while (n != 0) {
                if (i == idx) {
                    return n % 10;
                }
                i++;
                n /= 10;
            }
            return -1;
        }
    
        private int mySqrt(int x, int n) {
            int res = 1, temp = x;
            while (n != 0) {
                if ((n & 1) == 1) {
                    res *= temp;
                }
                temp *= temp;
                n >>>= 1;
            }
            return res;
        }
    }


    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.isPalindrome(121));        
        // System.out.println(s.mySqrt(10, 8));
    }
}
